package com.zzyl.nursing.service.impl;

import java.util.Collections;
import java.util.List;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;

import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class NursingPlanServiceImpl implements INursingPlanService 
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        //1、查询护理计划基础信息
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        //2、查询护理计划和护理项目关系信息
        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingProjectPlanMapper.selectByPlanId(id);
        if (!CollectionUtils.isEmpty(nursingProjectPlanVoList)){
            nursingPlanVo.setProjectPlans(nursingProjectPlanVoList);
        }

        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertNursingPlan(NursingPlanDto dto)
    {
        //1、保存护理计划基本信息
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        nursingPlan.setCreateTime(DateUtils.getNowDate());
        nursingPlanMapper.insertNursingPlan(nursingPlan);

        //2、批量保存护理计划和护理项目关系信息
            //获取保存护理计划和护理项目关系信息集合
        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        //判断集合是否为空
        if (CollectionUtils.isEmpty(projectPlans)){
         return 0;
        }
        int count = nursingProjectPlanMapper.batchInsert(nursingPlan.getId(),projectPlans);
        return count==0? 0:1;
    }

    /**
     * 修改护理计划
     * 
     * @param dto 护理计划
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateNursingPlan(NursingPlanDto dto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setUpdateTime(DateUtils.getNowDate());
        //属性拷贝
        BeanUtils.copyProperties(dto, nursingPlan);

        //获取护理计划与护理项目的关系集合
        List<NursingProjectPlanDto> projectPlans = dto.getProjectPlans();
        //判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
        if (!CollectionUtils.isEmpty(projectPlans)){
            //删除护理计划与护理项目的关系
            nursingProjectPlanMapper.deleteByPlanId(dto.getId());
            //批量添加护理计划与护理项目的关系
            nursingProjectPlanMapper.batchInsert(dto.getId(),projectPlans);
        }

        //别管项目列表是否为空，都要修改护理计划
        return nursingPlanMapper.updateNursingPlan(nursingPlan);

    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        return nursingPlanMapper.deleteNursingPlanByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        //根据Id删除护理计划的基本信息
        int count = nursingPlanMapper.deleteNursingPlanById(id);
        //删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        return count;
    }

    /**
     * 查询所有护理计划
     *
     * @return
     */
    @Override
    public List<NursingPlan> selectAllNursingPlan() {

        List<NursingPlan> nursingPlanList= nursingPlanMapper.selectAllNursingPlan();
        return nursingPlanList;

    }
}
